package com.xxk.wdis.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxk.wdis.entity.DrugStock;
import com.xxk.wdis.service.IDrugStockService;
import com.xxk.wdis.utills.HisResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 药品库存表 前端控制器
 * </p>
 *
 * @author cwb
 * @since 2020-04-09
 * <p>
 * 新增
 * <p>
 * 查询
 * 按药品编号查询 /drug-stock/stockCheck
 * 按五笔码或拼音码查询
 */
@Slf4j
@RestController
@RequestMapping("/drug-stock")
public class DrugStockController {
    @Autowired
    IDrugStockService iDrugStockService;

    /**
     * 库存：新增
     *
     * @param drugStock
     * @return
     */
    public Object insert(DrugStock drugStock) {
        try {
            if (iDrugStockService.save(drugStock)) {
                //新增成功
                return HisResult.ok(drugStock);
            } else {
                // 新增失败
                return HisResult.error("新增失败");
            }
        } catch (Exception e) {
            log.error("库存新增出错", e);
            return HisResult.error("库存新增出错", e.getMessage());
        }
    }

    /**
     * 库存查询
     *
     * @return
     */
    @GetMapping("/stockCheck")
    public Object stockCheck(
            @RequestParam(value = "drugId", defaultValue = "") Long drugId
    ) {
        QueryWrapper<DrugStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("drug_id", drugId);
        List<DrugStock> list = iDrugStockService.list(queryWrapper);
        return HisResult.ok(list);
    }
}

